from rest_framework import serializers

from car import models


class CarSerializer(serializers.ModelSerializer):
    parent = serializers.SerializerMethodField()

    def to_representation(self, instance):
        data = super().to_representation(instance)
        data['count'] = 0
        return data

    def get_parent(self, obj):
        return obj.parent.name

    class Meta:
        model = models.Car
        fields = '__all__'


class CarBrandSerializer(serializers.ModelSerializer):
    user = serializers.SerializerMethodField()
    parent = serializers.SerializerMethodField()

    def get_user(self, obj):
        return obj.user.username

    def get_parent(self, obj):
        return obj.parent.name

    class Meta:
        model = models.Car
        fields = '__all__'


class HotCarSerializer(serializers.ModelSerializer):
    son = CarSerializer(many=True)

    class Meta:
        model = models.Car
        fields = '__all__'
